using System;
using System.Diagnostics;

namespace MySqlBackUp.Server.Core
{

    public class CmdHelper
    {
        private string _logsContent; //日志内容
        private readonly string _cmd; //待执行的cmd命令

        public CmdHelper(string cmd)
        {
            _cmd = cmd;
        }

        /// <summary>
        /// 执行cmd命令
        /// </summary>
        public void RunCmd()
        {
            lock (_cmd)
            {
                try
                {
                    Process p = new Process();
                    //设置要启动的应用程序
                    p.StartInfo.FileName = "cmd.exe";
                    //是否使用操作系统shell启动
                    p.StartInfo.UseShellExecute = false;
                    // 接受来自调用程序的输入信息
                    p.StartInfo.RedirectStandardInput = true;
                    //输出信息
                    p.StartInfo.RedirectStandardOutput = true;
                    // 输出错误
                    p.StartInfo.RedirectStandardError = true;
                    //不显示程序窗口
                    p.StartInfo.CreateNoWindow = true;
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

                    p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                    p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
                    //启用Exited事件
                    p.EnableRaisingEvents = true;
                    p.Exited += new EventHandler(Process_Exited);

                    //启动程序
                    p.Start();
                    p.BeginOutputReadLine();
                    p.BeginErrorReadLine();
                    p.StandardInput.AutoFlush = true;

                    //输入命令
                    p.StandardInput.WriteLine(_cmd);
                    p.StandardInput.WriteLine("exit");
                }
                catch (Exception ex)
                {
                    FilesHelper.AppendTxt("CMD", $"执行cmd命令出错：\r\ncmd:{_cmd}\r\n{ex.Message}");
                }
            }
        }


        private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                SetLogContent(e.Data);
            }
        }

        private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                SetLogContent(e.Data);
            }
        }

        private void Process_Exited(object sender, EventArgs e)
        {
            SetLogContent("命令执行完毕");
            WriteTxt();
        }

        /// <summary>
        /// 设置日志内容
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        private void SetLogContent(string log)
        {
            _logsContent += log + "\r\n";
            Console.WriteLine(log);
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        private void WriteTxt()
        {
            FilesHelper.AppendTxt(_logsContent);
        }
    }
}